<template>
  <div class="tableList" ref="tableList">
    <a-table
      :columns="tableColumns"
      :data-source="state.dataSource"
      :scroll="state.tableScroll"
      :pagination="false"
      size="small"
      bordered
      :rowKey="rowKey"
      :customRow="rowClick"
      :rowClassName="setRowClassName"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'wptn'">
          <span class="redText wptnSpan" v-if="record.wptn == 1"> ↑ </span>
          <span class="blueText wptnSpan" v-if="record.wptn == 0"> ㅡ </span>
          <span class="greenText wptnSpan" v-if="record.wptn == 2"> ↓ </span>
        </template>
        <template v-if="column.dataIndex === 'tm'">
          {{ record.tm && record.tm.length > 11 ? record.tm.substr(5, 11) : record.tm }}
        </template>
        <template v-if="column.dataIndex === 'tmMin'">
          {{
            record.tmMin && record.tmMin.length > 11
              ? record.tmMin.substr(5, 11)
              : record.tmMin
          }}
        </template>
        <template v-if="column.dataIndex === 'minZTm'">
          {{
            record.minZTm && record.minZTm.length > 11
              ? record.minZTm.substr(0, 10)
              : record.minZTm
          }}
        </template>
        <template v-if="column.dataIndex === 'minQTm'">
          {{
            record.minQTm && record.minQTm.length > 11
              ? record.minQTm.substr(0, 10)
              : record.minQTm
          }}
        </template>
        <template v-if="column.dataIndex === 'maxZTm'">
          {{
            record.maxZTm && record.maxZTm.length > 11
              ? record.maxZTm.substr(0, 10)
              : record.maxZTm
          }}
        </template>
        <template v-if="column.dataIndex === 'maxQTm'">
          {{
            record.maxQTm && record.maxQTm.length > 11
              ? record.maxQTm.substr(0, 10)
              : record.maxQTm
          }}
        </template>
      </template>
    </a-table>
    <!-- 加载更多 -->
    <div
      class="pagination"
      :class="{ isShowPagination: !state.isShowPagination }"
      @click="handleClickNextPage"
    >
      <Icon icon="solar:alt-arrow-down-line-duotone" size="24" />
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, watch, inject } from "vue";
import { Icon } from "@gui-pkg/components";

defineOptions({ name: "TableList" });
const emit = defineEmits(["cellClick"]);
const tableList = ref(null);
const state = reactive({
  pageNum: 1,
  tableScroll: { y: 475 },
  isShowPagination: false,
  tableAllData: [],
  dataSource: [],
  lightIndex: -1,
});

const props = defineProps({
  tableDataSource: { type: Object },
  tableColumns: { type: Object },
  rowKey: { type: String },
});

const setRowClassName = (record, index) => {
  if (index == state.lightIndex) {
    return "rowcolor";
  } else {
    return;
  }
};

const rowClick = (record, index) => {
  return {
    onClick: () => {
      if (index == state.lightIndex) {
        state.lightIndex = -1;
      } else {
        state.lightIndex = index;
      }
      cellClick(record);
    },
  };
};

const cellClick = (params) => {
  emit("cellClick", { params, index: state.lightIndex });
};

// 前端分页
const handleClickNextPage = () => {
  let tableAllData = state.tableAllData;
  let pageNum = state.pageNum;
  if (tableAllData.length > 100 * state.pageNum) {
    state.isShowPagination = true;
    state.dataSource = tableAllData.slice(0, 100 * state.pageNum);
    pageNum++;
    state.pageNum = pageNum;
  } else {
    state.isShowPagination = false;
    state.dataSource = tableAllData;
  }
};

// 首次加载table数据
watch(
  () => props.tableDataSource,
  (data) => {
    state.tableAllData = data;
    if (state.tableAllData.length > 100) {
      state.isShowPagination = true;
      state.dataSource = data.slice(0, 100);
      state.tableScroll.y = tableList.value.offsetHeight - 60;
    } else {
      state.dataSource = data;
      state.isShowPagination = false;
    }
  }
);

// 监听是否点击了全屏，重新渲染下
const modalHeightChange = inject("ModalHeightChange");
watch(
  () => modalHeightChange.value,
  (val) => {
    state.tableScroll.y = val - 205;
  }
);
</script>

<style lang="less" rel="stylesheet/less" scoped>
.tableList {
  width: 100%;
  height: 100%;
  transform: translateX(0);
  transition: all 0.5s;
  position: relative;
  ::v-deep(.ant-table) {
    .ant-table-thead > tr > th {
      padding: 8px 0px !important;
    }

    .ant-table-container > .ant-table-body > table > tbody > tr > td {
      padding: 0px 6px !important;
      height: 34px;
    }
    .rowcolor {
      font-weight: 700;
    }
    p {
      margin: 0 !important;
    }
  }
  .pagination {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 24px;
    border-top: 1px solid #f0f0f0;
    position: absolute;
    left: 0;
    bottom: 0px;
    z-index: 100;
    background: #fff;
    .iconify {
      color: #ccc;
      cursor: pointer;
    }
  }
  .isShowPagination {
    opacity: 0;
  }
}

.wptnSpan {
  &.redText {
    color: red;
  }
  &.blueText {
    color: #0070ff;
  }
  &.greenText {
    color: green;
  }
}
</style>
